import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { setupRouterGuard } from './guard'
import BasicLayout from '@/layouts/BasicLayout.vue'

// 导入路由模块
import systemRoutes from './modules/system'
import configRoutes from './modules/config'
import profileRoutes from './modules/profile'

// 路由元数据类型扩展
declare module 'vue-router' {
  interface RouteMeta {
    title?: string
    icon?: string
    requiresAuth?: boolean
    permissions?: string[]
    keepAlive?: boolean
    hidden?: boolean
    hideInMenu?: boolean
    orderNo?: number
  }
}

// 基础路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: 'login.title', hidden: true }
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: { title: 'error.notFound', hidden: true }
  },
  {
    path: '/',
    component: BasicLayout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: 'menu.dashboard', icon: 'DashboardOutlined', requiresAuth: true }
      },
      {
        path: 'notification',
        name: 'Notification',
        component: () => import('@/views/notification/index.vue'),
        meta: { title: 'menu.notification.title', icon: 'NotificationOutlined', requiresAuth: true }
      }
    ]
  },
  // 添加系统路由到基础路由中
  systemRoutes,
  // 添加配置路由到基础路由中
  configRoutes,
  // 添加文件管理路由
  {
    path: '/file',
    component: BasicLayout,
    children: [
      {
        path: 'index',
        name: 'File',
        component: () => import('@/views/file/index.vue'),
        meta: { title: 'menu.file.title', icon: 'file', requiresAuth: true, permissions: ['file:list'] }
      }
    ]
  },
  // 404页面必须放在最后
  { path: '/:pathMatch(.*)*', redirect: '/404', meta: { hidden: true } }
]

// 异步路由（需要根据权限动态加载）
export const asyncRoutes: RouteRecordRaw[] = [
  // 使用模块化路由
  profileRoutes,
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes,
  scrollBehavior: () => ({ top: 0 })
})

// 设置路由守卫
setupRouterGuard(router)

export default router 